<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib uri="/authtaglib" prefix="auth"%>
<!DOCTYPE html>
<html>
<head>
<title>人员管理</title>
<jsp:include page="/include.jsp" />
<script src="<%=request.getContextPath()%>/js/print/print.js"></script>

<object id="csharpActiveX"
	classid="clsid:FFD3E742-47CD-4E67-9613-1BB0D67554FF"
	style="display: none;"></object>

<script type="text/javascript">

            var Com=9;
          
var activeX = document.getElementById("csharpActiveX");





//新增数据源配置信息窗口
function doAddDatasource() {
    $('<div id="addDiv"/>').dialog({
        href: '${pageContext.request.contextPath}/cnbdw/employeeAction!toAddPage.action',
        top: 10,
        width: 700,
        modal: true,
        title: '新增人员',
        buttons: [
            {
                id: 'btnSave',
                text: '保存',
                iconCls: '',
                handler: function () {
                    var d = $(this).closest('.window-body');
                    $('#add_form').form('submit', {
                        url: '${pageContext.request.contextPath}/cnbdw/employeeAction!add.action',
                        onSubmit: function () {
                            if ($(this).form("validate")) {
                                $.messager.progress({title: '提示', text: '正在保存，请稍候 ....'});
                            } else {
                                return false;
                            }
                        },
                        success: function (result) {
                            try {
                                var r = $.parseJSON(result);
                                if (r.returnFlag) {
                                    $('#datagrid').datagrid('reload');
                                    d.dialog('destroy');
                                }
                                //显示提示信息
                                $.messager.show({
                                    title: '提示',
                                    msg: r.returnMsg
                                });
                            } catch (e) {
                            }
                            $.messager.progress('close');
                        }
                    });
                }
            },
            {
                id: 'btnCancle',
                text: '取消',
                iconCls: '',
                handler: function () {
                    $(this).closest('.window-body').dialog('destroy');
                }
            }
        ],
        onClose: function () {
            $(this).dialog('destroy');
            //刷新主列表
            $('#datagrid').datagrid('reload');
        }
    });
}

function doBatchAddDatasource(){
	$('<div id="batchAddDiv"/>').dialog({
        href: '${pageContext.request.contextPath}/cnbdw/employeeAction!toBatchAddPage.action',
        top: 10,
        width: 700,
        modal: true,
        title: '批量导入人员',
        buttons: [
           {
                id: 'btnSave',
                text: '保存',
                iconCls: '',
                handler: function () {
                    var d = $(this).closest('.window-body');
                    $('#add_form').form('submit', {
                        url: '${pageContext.request.contextPath}/cnbdw/employeeAction!batchAdd.action',
                        onSubmit: function () {
                            if ($(this).form("validate")) {
                                $.messager.progress({title: '提示', text: '正在保存，请稍候 ....'});
                            } else {
                                return false;
                            }
                        },
                        success: function (result) {
                           try {
                                var r = $.parseJSON(result);
                                if (r.returnFlag) {
                                    $('#datagrid').datagrid('reload');
                                    d.dialog('destroy');
                                }
                                //显示提示信息
                                $.messager.show({
                                    title: '提示',
                                    msg: r.returnMsg
                                });
                            } catch (e) {
                            }
                            $.messager.progress('close');
                        }
                    });
                }
            },
            {
                id: 'btnCancle',
                text: '取消',
                iconCls: '',
                handler: function () {
                    $(this).closest('.window-body').dialog('destroy');
                }
            }
        ],
        onClose: function () {
            $(this).dialog('destroy');
            //刷新主列表
            $('#datagrid').datagrid('reload');
        }
    });
}

//修改数据源配置信息窗口
function doEditDatasource(id) {
    $('<div id="addDiv"/>').dialog({
        href: '${pageContext.request.contextPath}/cnbdw/employeeAction!toEditPage.action?id=' + id,
        top: 10,
        width: 700,
        modal: true,
        title: '修改人员',
        buttons: [
            {
                id: 'btnSave',
                text: '保存',
                iconCls: '',
                handler: function () {
                    var d = $(this).closest('.window-body');
                    $('#add_form').form('submit', {
                        url: '${pageContext.request.contextPath}/cnbdw/employeeAction!update.action',
                        onSubmit: function () {
                            if ($(this).form("validate")) {
                                $.messager.progress({title: '提示', text: '正在保存，请稍候 ....'});
                            } else {
                                return false;
                            }
                        },
                        success: function (result) {
                            try {
                                var r = $.parseJSON(result);
                                if (r.returnFlag) {
                                    $('#datagrid').datagrid('reload');
                                    d.dialog('destroy');
                                }
                                //显示提示信息
                                $.messager.show({
                                    title: '提示',
                                    msg: r.returnMsg
                                });
                            } catch (e) {
                            }
                            $.messager.progress('close');
                        }
                    });
                }
            },
            {
                id: 'btnCancle',
                text: '取消',
                iconCls: '',
                handler: function () {
                    $(this).closest('.window-body').dialog('destroy');
                }
            }
        ],
        onClose: function () {
            $(this).dialog('destroy');
            //刷新主列表
            $('#datagrid').datagrid('reload');
        }
    });
}
//删除
function doDeleteDatasource(ids) {
    window.top.$.messager.confirm('提示', '是否确定删除该条记录？', function (b) {
        if (b) {
            $.ajax({
                url: '${pageContext.request.contextPath}/cnbdw/employeeAction!delete.action',
                dataType: 'json',
                data: {ids: ids},
                success: function (data) {
                    $.messager.show({title: '提示', msg: data.returnMsg});
                    if (data.returnFlag) {
                        //刷新主列表
                        $('#datagrid').datagrid('reload');
                    }
                }
            });

        }
    });
}

//批量删除
function doBatchDelete() {
    var rows = $('#datagrid').datagrid('getChecked')
    if (rows.length <= 0) {
        $.messager.alert('提示', '没有选择任何记录');
    } else {
        var itemIds = [];
        $.each(rows, function (i, item) {
            itemIds.push(item.id);
        });
        $.messager.confirm('删除确认', '是否确定删除选中记录？', function (r) {
            if (r) {
                $.ajax({
                    url: '${pageContext.request.contextPath}/cnbdw/employeeAction!delete.action',
                    async: false,
                    data: {ids: itemIds.join(",")},
                    dataType: "json",
                    success: function (result) {
                        $.messager.show({title: '提示', msg: result.returnMsg});
                       
                            //刷新主列表
                            $('#datagrid').datagrid('reload');
                        
                    }
                });
            }
        });
    }
}

//执行查询
function doQuery() {
    if ($("#datasource_query_form").form("validate")) {
        $("#datagrid").datagrid('options').queryParams = serializeObject($('#datasource_query_form')); //把查询条件赋值给datagrid内部变量
        $("#datagrid").datagrid('load'); //重新加载
    }
}
//重置查询条件
function doReset() {
    $('#datasource_query_form').form('reset');
}
//导出

function doExport(){
	window.location.href='${pageContext.request.contextPath}/cnbdw/employeeAction!exportDatasourceList.action?name='+$("#datasource_datsourcename").val();
}
function doPrint(){
	var row = $('#datagrid').datagrid('getSelected')
    if (row == null) {
        $.messager.alert('温馨提示', '请选中一个需要绑定设备的人员');
        return;
    }
	//window.print();
	window.location.href='${pageContext.request.contextPath}/cnbdw/employeeAction!printDatasourceList.action?empId='+row.id;
	//CreateFormPage("datagrid",$("#datagrid"));
}

//页面加载完后响应 onReady
$(function () {
    //查询按纽绑定事件
    $('#btnQuery').bind('click', doQuery);
    //重置按纽绑定事件
    $('#btnReset').bind('click', doReset);
    //重置按纽绑定事件
    
    //初始化主列表
    $('#datagrid').datagrid({
        url: '${pageContext.request.contextPath}/cnbdw/employeeAction!stayDatasource.action',
        fit: true,
        border: false,
        fitColumns: true,
        pagination: true,
        pageList: [30],
        rownumbers: false,
        sortName: 'id',
        sortOrder: 'desc',
        striped: true,
        columns: [
            [
                {field: 'id', title: '唯一编号', align: 'left',checkbox:'true', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}},
                {field: 'photo', title: '照片', align: 'left', formatter: function (value, rowData, rowIndex) {
                    if ($.trim(value) != '') {
                        return '<img src="${pageContext.request.contextPath}' + value + '" style="width: 30px; height: 30px;">';
                    }
                    return '--';
                }},
                {field: 'name', title: '姓名', align: 'left', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}},
                //{field: 'namePy', title: 'namePy', align: 'left', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}},
                {field: 'gender', title: '性别', align: 'left', formatter: function (value, rowData, rowIndex) {
                    if (value == 1) {
                        return '男'
                    } else{
                        return '女'
                    }
                    return value;
                }},
                 {field: 'level', title: '人员级别', align: 'left', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}},
                {field: 'company', title: '单位', align: 'left', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}},
//                {field: 'duties', title: '政务', align: 'left', formatter: function (value, rowData, rowIndex) {
//                    if (value == '1') {
//                        return '长官'
//                    } else{
//                        return '士兵'
//                    }
//                    return value;
//                }},
                {field: 'cardType', title: '证件类型', align: 'left', formatter: function (value, rowData, rowIndex) {
                    if (value == 1) {
                        return '身份证'
                    } else if(value == 2){
                        return '军官证'
                    }
                    return value;
                }},
                {field: 'cardNo', title: '证件号码', align: 'left', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}},
                {field: 'companyr', title: '入住信息', align: 'left', formatter: function (value, rowData, rowIndex) {
                    if (rowData.bedInfo == null) {
                        return '未入住'
                    } else {
                        var _htmls = [];
                        _htmls.push(getRoomNo(rowData.bedInfo, $('#datagrid').datagrid('getData')));
                        _htmls.push(getBadNum(rowData.bedInfo));
                        return _htmls.join("");
                    }
                    return value;
                }},
                {field: 'duties', title: '已绑定设备', align: 'left', formatter: function (value, rowData, rowIndex) {
                    if (rowData.employeeDevices == null || rowData.employeeDevices.length == 0) {
                        return '未绑定任何设备'
                    } else {
                        var _htmls = [];
                        $.each(rowData.employeeDevices, function (index, edevice) {
                            if (edevice.device) {
                                var deviceType = edevice.device.deviceType;
                                if (index != 0) {
                                    _htmls.push(", ");
                                }

                                if (deviceType == 1) {
                                    _htmls.push("手表");
                                } else if(deviceType == 2) {
                                    _htmls.push("手环1");
                                }else if(deviceType == 3) {
                                    _htmls.push("手环2");
                                }else if(deviceType == 4) {
                                    _htmls.push("智能卡");
                                }else if(deviceType == 5) {
                                    _htmls.push("人员卡");
                                }

                                _htmls.push("(设备名:");
                                _htmls.push(edevice.device.cardNum);
                                _htmls.push(")");
                            }
                        });
                        return _htmls.join("")
                    }
                    return value;
                    return value;
                }},
                {field: 'stayTime', title: '入住时间', align: 'left', formatter: function (value, rowData, rowIndex) {
                    if (rowData.bedInfo== null) {
                        return '--'
                    } else {
                         var _htmls = [];
                        
                        _htmls.push(getStayTime(rowData.bedInfo));
                        return _htmls.join("");
                    }
                    return value;
                }},
                {field: 'outTime', title: '退房时间', align: 'left', formatter: function (value, rowData, rowIndex) {
                    if (rowData.bedInfo == null) {
                        return '--'
                    } else {
                         var _htmls = [];
                       
                        _htmls.push(getOutTime(rowData.bedInfo));
                        return _htmls.join("");
                    }
                    return value;
                }},
//                {field: 'level', title: '级别', align: 'left', formatter: function (value, rowData, rowIndex) {
//                    if (value == 1) {
//                        return 'A类'
//                    }else if (value == 2) {
//                        return 'B类'
//                    }else if (value == 3) {
//                        return 'C类'
//                    }else if (value == 4) {
//                        return 'D类'
//                    }
//                    return value;
//                }},

          //      {field: 'recordPosition', title: '记录位置', align: 'left', formatter: function (value, rowData, rowIndex) {
          //          if (value == 1) {
          //              return '是'
          //          }
          //          return '否';
          //      }},
             //   {field: 'createTime', title: '创建时间', align: 'left', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}},
             //   {field: 'createUser', title: '创建人', align: 'left', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}},
                {field: 'updateTime', title: '修改时间', align: 'left', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}},
                {field: 'updateUser', title: '修改人', align: 'left', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}},
                {field: 'action', title: '人员操作', align: 'left',
                    formatter: function (value, row, index) {
                        var html = "";
                        <auth:button authUrl="cnbdw/employeeAction!update.action">
                        html += '<span  style="cursor:pointer" onclick="doEditDatasource(\'' + row.id + '\')"><img  src="${pageContext.request.contextPath}/images/icons/modify_or_edit.png" title="修改" alt="修改"/>修改</span>';
                        </auth:button>
                        <auth:button authUrl="cnbdw/employeeAction!delete.action">
                        html += '<span style="cursor:pointer" onclick="doDeleteDatasource(\'' + row.id + '\')"><img src="${pageContext.request.contextPath}/images/icons/delete.png" title="删除" alt="删除" style="margin-top:2px;">删除</span>';
                        </auth:button>
                        return html;
                    }
                }
            ]
        ],
        toolbar: '#datagrid_toobar'
    });
    
    
    
    $('#list_select_empTypeinfo').combobox({
            url:'${pageContext.request.contextPath}/levelAction!getLevelSelectData.action',
            valueField:'id',
            textField:'name',
            editable:false
        });
});


function getRoomNo(bed, data) {
    var room = bed.room;
    if(bed.room && bed.room.$ref){
        room = jsonPath(data, bed.room.$ref);
        return "[" + room[0].roomNo + "]号房";
    }
    if(room != null){
        return "[" + room.roomNo + "]号房";
    }
    return "--";
}
function getBadNum(bed) {
    if (bed == null) {
        return "--";
    }

    return badNum = "[" + bed.bedNo + "]床";
}
function getStayTime(bed) {
   // if (bed.stayTime == null) {
  //      return "未办理房卡";
//    }

    return stayTime =  bed.stayTime;
}
function getOutTime(bed) {
   // if (bed.outTime == null) {
    //    return "未办理房卡";
  //  }

    return outTime =  bed.outTime;
}



function showFileUpload(callbackId) {
    window.top.$('<div/>').dialog({
        href: '${pageContext.request.contextPath}/cnbdw/commons/file_upload.jsp',
        modal: true,
        top: ($(window.top).height() - 400) * 0.5,
        left: ($(window.top).width() - 600) * 0.5,
        width: 400,
        title: '上传附件',
        buttons: [
            {
                text: '保存',
                iconCls: '',
                id: 'fileUploadDialog',
                handler: function () {
                    //获取专题性别参数
                    var d = window.top.$(this).closest('.window-body');
                    if (window.top.fileValidateData()) {
                        window.top.$('#fileUploadForm').form('submit', {
                            url: '${pageContext.request.contextPath}/fileUploadAction!uploadFile.action',
                            success: function (result) {
                                try {
                                    var r = $.parseJSON(result);
                                    if (r.err != null && r.err != '') {
                                        //显示提示信息
                                        $.messager.show({
                                            title: '提示',
                                            msg: r.err
                                        });
                                    } else {
                                        $('#' + callbackId).val(r.msg);
                                        $('#photoPanel').attr('src', '${pageContext.request.contextPath}' + r.msg);
                                        //关闭新增窗口
                                        d.dialog('destroy');
                                    }
                                } catch (e) {
                                    $.messager.alert('提示', result);
                                }
                            }
                        });
                    }
                }
            }
        ],
        onClose: function () {
            window.top.$(this).dialog('destroy');
        }
    });
}


function empBindDevice(){
	var row = $('#datagrid').datagrid('getSelected')
    if (row == null) {
        $.messager.alert('温馨提示', '请选中一个需要绑定设备的人员');
        return;
    }
    var cardsn;
    var activeReadCardSN=  activeX.ReadCardSN(Com);
     if(activeReadCardSN==0){
     //  cardsn= activeX.ReadCardSN_CardSN;
      var readBlock=activeX.ReadBlock(9,0,0,"FFFFFFFFFFFF");
    var readRecardData=activeX.ReadRecordData();
    cardsn=readRecardData.substring(0,8).toUpperCase();
      }else{
      cardsn="";
      }
	$('<div id="addDiv"/>').dialog({
        href: '${pageContext.request.contextPath}/cnbdw/employeeAction!empBindDevicePage.action?empId='+row.id+'&cardNum='+cardsn,
        top: 10,
        width: 700,
        modal: true,
        title: '['+row.name+']'+'设备绑定',
        buttons: [
            {
                id: 'btnSave',
                text: '保存',
                iconCls: '',
                handler: function () {
                    var d = $(this).closest('.window-body');
                   // var empId=$("#empId").val();
                   // alert("empId>>"+empId);
                    var empId=$("#empId").val();
                   
                    var rows =  $("#bind-datagrid").datagrid("getSelections");
                    if(rows.length<1){
                    	$.messager.show({
                            title: '提示',
                            msg: "请选择设备!"
                        });
                    	return;
                    }
                    var ids = [];
                    for(var i=0; i<rows.length; i++){
                    	ids.push(rows[i].id);
                    }
                   // alert("ids>>>"+ids.join(','));
                    $.ajax({
                    	type:"POST",
        	            url: '${pageContext.request.contextPath}/employeeAction!doBindDevice.action',
        	            dataType: 'json',
        	            data: {ids:ids.join(','),empId:empId},
        	            success: function (result) {
        	            	try {
                                var r = result;
                                if (r.returnFlag) {
                                    $('#datagrid').datagrid('reload');
                                    d.dialog('destroy');
                                }
                                //显示提示信息
                                $.messager.show({
                                    title: '提示',
                                    msg: r.returnMsg
                                });
                            } catch (e) {
                            }
                            $.messager.progress('close');
        	            }
        	        });
               
                }
            },
            {
                id: 'btnCancle',
                text: '取消',
                iconCls: '',
                handler: function () {
                    $(this).closest('.window-body').dialog('destroy');
                }
            }
        ],
        onClose: function () {
            $(this).dialog('destroy');
            //刷新主列表
            $('#datagrid').datagrid('reload');
        }
    });
}


function empRelieveDevice(){
		var row = $('#datagrid').datagrid('getSelected');
    if (row == null) {
      var activeReadCardSN=  activeX.ReadCardSN(Com);
      if(activeReadCardSN==0){
      
    //  var cardsn= activeX.ReadCardSN_CardSN;
    
    
    var readBlock=activeX.ReadBlock(9,0,0,"FFFFFFFFFFFF");
    var readRecardData=activeX.ReadRecordData();
   var cardsn=readRecardData.substring(0,8).toUpperCase();
    
    
      $('<div id="addDiv"/>').dialog({
        href: '${pageContext.request.contextPath}/cnbdw/employeeAction!deviceRelieveEmpPage.action?cardSn='+cardsn,
        top: 10,
        width: 800,
        modal: true,
        title: '['+cardsn+']'+'解除绑定',
        buttons: [
            {
                id: 'btnSave',
                text: '解绑',
                iconCls: '',
                handler: function () {
                    var d = $(this).closest('.window-body');
                  //  var empId=$("#empId").val();
                   // alert("empId>>"+empId);
                    var empId=$("#empId").val();
                    if(empId==null){
                    $.messager.show({
                            title: '提示',
                            msg: "此设备未绑定人员!"
                        });
                    	return;
                    }
                    var empName=$("#empName").val();
                    var reason=$("#reason").val();
                    var rows =  $("#bind-datagrid").datagrid("getSelections");
                    if(rows.length<1){
                    	$.messager.show({
                            title: '提示',
                            msg: "请选择设备!"
                        });
                    	return;
                    }
                    var ids = [];
                    for(var i=0; i<rows.length; i++){
                    	ids.push(rows[i].id);
                    }
                    $.ajax({
                    	type:"POST",
        	            url: '${pageContext.request.contextPath}/employeeAction!doRelieveDevice.action',
        	            dataType: 'json',
        	            data: {ids:ids.join(','),empId:empId,reason:reason},
        	            success: function (result) {
        	            	try {
                                var r = result;
                                if (r.returnFlag) {
                                    $('#datagrid').datagrid('reload');
                                    d.dialog('destroy');
                                }
                                //显示提示信息
                                $.messager.show({
                                    title: '提示',
                                    msg: r.returnMsg
                                });
                            } catch (e) {
                            }
                            $.messager.progress('close');
        	            }
        	        });
               
                }
            },
            {
                id: 'btnCancle',
                text: '取消',
                iconCls: '',
                handler: function () {
                    $(this).closest('.window-body').dialog('destroy');
                }
            }
        ],
        onClose: function () {
            $(this).dialog('destroy');
            //刷新主列表
            $('#datagrid').datagrid('reload');
        }
    });
      
      
      
      }else{
       $.messager.alert('温馨提示', '请放入卡片设备');
         return;
      }
        
    }else{
    var empId=row.id;
    
   // alert(empId+"$$$");
	$('<div id="addDiv"/>').dialog({
        href: '${pageContext.request.contextPath}/cnbdw/employeeAction!empRelieveDevicePage.action?empId='+empId,
        top: 10,
        width: 800,
        modal: true,
        title: '['+row.name+']'+'解除绑定',
        buttons: [
            {
                id: 'btnSave',
                text: '解绑',
                iconCls: '',
                handler: function () {
                    var d = $(this).closest('.window-body');
                  //  var empId=$("#empId").val();
                   // alert("empId>>"+empId);
                    var empId=$("#empId").val();
                    
                    var reason=$("#reason").val();
                    var rows =  $("#bind-datagrid").datagrid("getSelections");
                    if(rows.length<1){
                    	$.messager.show({
                            title: '提示',
                            msg: "请选择设备!"
                        });
                    	return;
                    }
                    var ids = [];
                    for(var i=0; i<rows.length; i++){
                    	ids.push(rows[i].id);
                    }
                    $.ajax({
                    	type:"POST",
        	            url: '${pageContext.request.contextPath}/employeeAction!doRelieveDevice.action',
        	            dataType: 'json',
        	            data: {ids:ids.join(','),empId:empId,reason:reason},
        	            success: function (result) {
        	            	try {
                                var r = result;
                                if (r.returnFlag) {
                                    $('#datagrid').datagrid('reload');
                                    d.dialog('destroy');
                                }
                                //显示提示信息
                                $.messager.show({
                                    title: '提示',
                                    msg: r.returnMsg
                                });
                            } catch (e) {
                            }
                            $.messager.progress('close');
        	            }
        	        });
               
                }
            },
            {
                id: 'btnCancle',
                text: '取消',
                iconCls: '',
                handler: function () {
                    $(this).closest('.window-body').dialog('destroy');
                }
            }
        ],
        onClose: function () {
            $(this).dialog('destroy');
            //刷新主列表
            $('#datagrid').datagrid('reload');
        }
    });
    }
}



function clearBindDevice(empId){
	 $.messager.confirm('操作确认', "操作会清除该人员绑定的设备,确认清除吗?", function (b) {
		if(b){
			$.ajax({
		    	type:"POST",
		        url: '${pageContext.request.contextPath}/employeeAction!clearBindDevice.action',
		        dataType: 'json',
		        data: {empId:empId},
		        success: function (result) {
		        	try {
		                var r = result;
		                if (r.returnFlag){
		                    $('#datagrid').datagrid('reload');
		                }
		                //显示提示信息
		                $.messager.show({
		                    title: '提示',
		                    msg: r.returnMsg
		                });
		            } catch (e) {
		            }
		            $.messager.progress('close');
		        }
		    });
		}
	});
	
}



function autoStayPage() {
    var row = $('#datagrid').datagrid('getSelected')
    if (row == null) {
        $.messager.alert('温馨提示', '请选中一个需要入住的人员');
        return;
    }
   // if (row.punch != 1) {
   //     $.messager.alert('温馨提示', '当前人员还未上船，请先上船操作');
   //     return;
  //  }
    $('<div id="addDiv"/>').dialog({
        href: '${pageContext.request.contextPath}/cnbdw/employeeAction!autoStayPage.action?id=' + row.id,
        top: 10,
        width: 700,
        modal: true,
        title: '人员入住',
        buttons: [
            {
                id: 'btnSave',
                text: '确认入住',
                iconCls: '',
                handler: function () {
              //  	var pp = $("#assign_tabs").tabs('getSelected').title;
               // 	var tab = pp.panel('options').tab;
               // 	alert("+1"+tab);    
               
               
              var index =$('#assign_tabs').tabs('getTabIndex', $('#assign_tabs').tabs('getSelected'));
               var bedrow; 
               var startStay;
               var endStay;
                if(index==0){
                	
                    bedrow = $("#stay-datagrid").datagrid('getSelected'); //重新加载
                    startStay=$("#startStay").datetimebox('getValue');
                   endStay=$("#endStay").datetimebox('getValue');
                   
                  if(startStay==null||startStay==""&&endStay==null||endStay==""){
                    $.messager.alert('温馨提示', '未选择入住及退房时间。');
                        return;
                   
                   }
              } else if(index == 1){
                    	 	bedrow = $("#stay-datagrid1").datagrid('getSelected'); //重新加载 手动
                    startStay=$("#startStay1").datetimebox('getValue');
                   
                   endStay=$("#endStay1").datetimebox('getValue');}
                    if (bedrow == null ) {
                        $.messager.alert('温馨提示', '没有可自动分配的床位，请选择手动分配。');
                        return;
                    }
                    if(startStay==null||startStay==""&&endStay==null||endStay==""){
                    $.messager.alert('温馨提示', '未选择入住及退房时间。');
                        return;
                   
                   }
                    /*if (bedrow1 == null ) {
                        $.messager.alert('温馨提示', '没有可自动分配的床位nnnnnnnnnnnn，请选择手动分配。');
                        return;
                    }*/
                    var empId = row.id, roomId, bedId;
                    var confirmMsg = "";
                   // alert("+++"+bedrow);
                    if (bedrow != null) {
                        var roomType = bedrow.roomNo ? bedrow.roomNo.name : "";
                    //    alert("-------"+roomType);
                     //    alert("-------"+bedrow.roomNo);
                     //     alert("-------"+bedrow.roomNo.name);
                        var bedInfos = bedrow.bedInfos;
                        if (bedInfos == null || bedInfos.length == 0) {
                            $.messager.alert('温馨提示', '当前房间没有多余床位，请重新选择。');
                            return;
                        }

                        var bed = null;
                        $.each(bedInfos, function (index, item) {
                            if (!item.empId) {
                                bed = item;
                                return false;
                            }
                        });
                        if (bed == null) {
                            $.messager.alert('温馨提示', '当前房间没有多余床位，请重新选择。');
                            return;
                        }
                        bedId = bed.id;
                        roomId = bed.roomId;
                        confirmMsg = "您将分配 <b>[" + bedrow.roomNo + "]房[" + bed.bedNo + "]床</b>分配给[" + row.name + "],您确定要这样做吗?";
                    } else if ($.trim($('#auto_bed_id').val()) != "") {
                        confirmMsg = "您系统自动挑选的 " + $('#auto_room_type').val() + "<b>[" + $('#auto_room_no').val() + "]房[" + $('#auto_bed_no').val() + "]床</b>分配给[" + $('#auto_emp_name').val() + "],您确定要这样做吗?";
                        bedId = $('#auto_bed_id').val();
                        roomId = $('#auto_room_id').val();
                    }
                    var d = $(this).closest('.window-body');
                    
                    
                    window.top.$.messager.confirm('操作确认', confirmMsg, function (b) {
                        if (b) {
                            $.messager.progress({title: '提示', text: '正在保存，请稍候 ....'});
                            $.ajax({
                                url: '${pageContext.request.contextPath}/cnbdw/employeeAction!stay.action',
                                dataType: 'json',
                                data: {
                                    empId: empId,
                                    bedId: bedId,
                                    roomId: roomId,
                                    startStay:startStay,
                                    endStay:endStay
                                    
                                },
                                success: function (result) {
                                    try {
                                        if (result.returnFlag) {
                                            $('#datagrid').datagrid('reload');
                                            d.dialog('destroy');
                                        }
                                        $.messager.show({title: '提示', msg: result.returnMsg});
                                    } catch (e) {
                                    }
                                },
                                complete: function () {
                                    $.messager.progress('close');
                                }
                            });

                        }
                    });
                }
            },
            {
                id: 'btnCancle',
                text: '取消',
                iconCls: '',
                handler: function () {
                    $(this).closest('.window-body').dialog('destroy');
                }
            }
        ],
        onClose: function () {
            $(this).dialog('destroy');
            //刷新主列表
            $('#datagrid').datagrid('reload');
        }
    });
}

function writeCard() {
    var row = $('#datagrid').datagrid('getSelected')
    if (row == null) {
        $.messager.alert('温馨提示', '请选中一个需要写卡的人员');
        return;
    }

    $('<div id="writeCardDiv"/>').dialog({
        href: '${pageContext.request.contextPath}/cnbdw/employeeAction!writing.action?id=' + row.id,
        top: 10,
        height:300,
        width: 400,
        modal: true,
        title: '写卡',
        onClose: function () {
            $(this).dialog('destroy');
        }
    });
}



function empDeviceHistory() {
 var row = $('#datagrid').datagrid('getSelected')
    if (row == null) {
        $.messager.alert('温馨提示', '请选中一个需要查看的人员');
        return;
    }
		
	$('<div id="addDiv"/>').dialog({
        href: '${pageContext.request.contextPath}/cnbdw/employeeDeviceHistoryAction!indexPage.action?id=' + row.id,
        top: 10,
        width: 700,
        height:500,
        modal: true,
        title: '人员绑定历史',
        buttons: [
            
            {
                id: 'btnCancle',
                text: '取消',
                iconCls: '',
                handler: function () {
                    $(this).closest('.window-body').dialog('destroy');
                }
            }
        ],
        onClose: function () {
            $(this).dialog('destroy');
            //刷新主列表
            $('#datagrid').datagrid('reload');
        }
    });


}



</script>
</head>
<body class="easyui-layout">
	<form id="datasource_query_form" method="post">
		<div data-options="region:'north'"
			style="height:120px;border-bottom:none;border-top:none;">
			<table border="0" cellpadding="0" cellspacing="0" height="100%"
				width="60%">


				<tr class="tr_left">
					<td style="width:1%;text-align:left;" nowrap="nowrap"
						class="tr_left"><label for="datasource_datsourcename">姓&nbsp;&nbsp;名：</label>
						<input type="text" id="datasource_datsourcename" name="name"
						class="easyui-validatebox"
						data-options="validType:'specialCharFilter'" /></td>
					<td style="width:1%;text-align:left;" nowrap="nowrap"
						class="tr_left"><label>开始时间：</label> <input id="start"
						name="start" type="text" class="easyui-datebox">&emsp;</td>
					<td style="width:1%;text-align:left;" nowrap="nowrap"
						class="tr_left"><label>结束时间：</label> <input id="end"
						name="end" type="text" class="easyui-datebox">&emsp;</td>

				</tr>
				<tr>
					<td style="width:1%;text-align:left;" nowrap="nowrap"
						class="tr_left"><label for="datasource_datsourcecompany">单&nbsp;&nbsp;位：</label>
						<input type="text" id="datasource_datsourcecompany" name="company"
						class="easyui-validatebox"
						data-options="validType:'specialCharFilter'" /></td>
					<td style="width:1%;text-align:left;" nowrap="nowrap"
						class="tr_left"><label for="list_select_deviceType">证件类型：</label>

						<input type="radio" name="cardType" value="2"
						class="easyui-validatebox"
						data-options="validType:'specialCharFilter'" />军官证 <input
						type="radio" name="cardType" value="1" class="easyui-validatebox"
						data-options="validType:'specialCharFilter'" />身份证</td>
					<td style="width:1%;text-align:left;" nowrap="nowrap"
						class="tr_left"><label for="datasource_datsourcecardnum">证件号码：</label>
						<input type="text" id="datasource_datsourcecardno" name="cardNo"
						class="easyui-validatebox"
						data-options="validType:'specialCharFilter'" /></td>

</tr>
<tr>
                <td style="width:1%;text-align:left;" nowrap="nowrap" class="tr_left">
                    <label for="datasource_datsourcecardnum">人员类型：</label>
                    <input type="text" id="list_select_empTypeinfo" name="empType"
                   		class="easyui-combobox" data-options="validType:'specialCharFilter'"/>
                </td>

                       <td style="width:1%;text-align:left;" nowrap="nowrap" 
						class="tr_left"><label for="list_select_deviceType">绑定状态：</label>

						<input type="radio" name="ifBangDevice" value="1"
						class="easyui-validatebox"
						data-options="validType:'specialCharFilter'" />未绑定设备人员 <input
						type="radio" name="ifBangDevice" value="0" class="easyui-validatebox"
						data-options="validType:'specialCharFilter'" />已绑定设备人员</td>



					<td colspan="2" style="width:10%;text-align:left;" nowrap="nowrap">
						<input type="button" id="btnQuery" value="查 询" class="search_btn" />


						<input type="button" id="btnReset" value="重 置" class="reset_btn" />
					</td>

				</tr>



			</table>
		</div>
		<div data-options="region:'center'" style="border-top:none">
			<table id="datagrid"></table>
			<div id="datagrid_toobar" class="etl_panel_title">
				<auth:button authUrl="cnbdw/employeeAction!add.action">
					<a href="javascript:void(0);" onclick="doAddDatasource()"
						class="easyui-linkbutton"
						data-options="iconCls:'icon-add',plain:true" style="float:left;">新增</a>

					<a href="javascript:void(0);" onclick="doBatchAddDatasource()"
						class="easyui-linkbutton"
						data-options="iconCls:'icon-tip',plain:true" style="float:left;">批量导入</a>

					<div class="datagrid-btn-separator"></div>

					<a href="javascript:void(0);" onclick="doBatchDelete()"
						class="easyui-linkbutton"
						data-options="iconCls:'icon-remove',plain:true"
						style="float:left;">批量删除</a>

					<div class="datagrid-btn-separator"></div>

					<a
						href="${pageContext.request.contextPath}/resources/emploeey_template.xls"
						data-options="iconCls:'icon-redo',plain:true"
						class="easyui-linkbutton" style="float:left;">导入模板下载</a>

					<a href="javascript:void(0)" onclick="doPrint()"
						data-options="iconCls:'icon-ok',plain:true"
						class="easyui-linkbutton" style="float:left;">打印</a>


					<a href="javascript:void(0)" onclick="doExport()"
						data-options="iconCls:'icon-undo',plain:true"
						class="easyui-linkbutton" style="float:left;">导出</a>

					<div class="datagrid-btn-separator"></div>

					<a href="javascript:void(0)" onclick="empBindDevice()"
						data-options="iconCls:'icon-mini-add',plain:true"
						class="easyui-linkbutton" style="float:left;">绑定设备</a>

					<a href="javascript:void(0)" onclick="empRelieveDevice()"
						data-options="iconCls:'icon-mini-edit',plain:true"
						class="easyui-linkbutton" style="float:left;">解绑设备</a>


					<div class="datagrid-btn-separator"></div>

					<a href="javascript:void(0);" onclick="autoStayPage()"
						class="easyui-linkbutton"
						data-options="iconCls:'icon-edit',plain:true" style="float:left;">入住</a>

					<div class="datagrid-btn-separator"></div>

					<a href="javascript:void(0);" onclick="writeCard()"
						class="easyui-linkbutton"
						data-options="iconCls:'icon-remove1',plain:true"
						style="float:left;">写卡</a>

					<div class="datagrid-btn-separator"></div>

					<a href="javascript:void(0);" onclick="empDeviceHistory()"
						class="easyui-linkbutton"
						data-options="iconCls:'icon-sum',plain:true" style="float:left;">人员绑定历史</a>

				</auth:button>
			</div>
		</div>
	</form>
</body>
</html>

